package tongcheng.com.tongcheng.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;

import java.util.List;

import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseViewHolder;
import tongcheng.com.tongcheng.widght.PhotoPopWindow;

/**
 * Created by P on 2017/7/18.
 */

public class ShigongAdapter extends RecyclerView.Adapter<ShigongAdapter.SendOrderViewholder> {
    public ShigongAdapter() {

    }

    public List<String> filepath;
    public Context mContext;
    public Activity mActivity;
    public RelativeLayout mView;
    public ShigongAdapter(List<String> filepath, Context context, Activity activity, RelativeLayout view) {
        this.filepath = filepath;
        this.mContext = context;
        this.mActivity = activity;
        this.mView = view;
    }
    @Override
    public SendOrderViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_sendorder, null);
        return new SendOrderViewholder(view);
    }

    @Override
    public void onBindViewHolder(SendOrderViewholder holder, int position) {
        if (filepath != null) {
            if (0 == filepath.size()) {
                holder.item_sendorder_add.setImageResource(R.mipmap.zhaopian_zhenjia);

                holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        takePhoto();
                    }
                });

            } else  {
                if (filepath.size() == position) {
                    holder.item_sendorder_add.setImageResource(R.mipmap.zhaopian_zhenjia);
                    holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            takePhoto();
                        }
                    });
                } else {
                    holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
//                            ShowToast.showToast("我被点了");
                        }
                    });
                    holder.item_sendorder_add.setBackgroundResource(android.R.color.transparent);
//                    Glide.with(mContext).load(Uri.fromFile(new File(filepath.get(position)))).into(holder.item_sendorder_add);
                    Log.e("上传图片22",filepath.get(position) + "");
                    Glide.with(mContext).load(filepath.get(position)).into(holder.item_sendorder_add);


                }
            }
        }

    }

    private void takePhoto() {
        PhotoPopWindow photoPopWindow = new PhotoPopWindow(mContext, mActivity);
        photoPopWindow.maxChooseNum = 8;
        photoPopWindow.show(mView);
    }

    @Override
    public int getItemCount() {

        return filepath==null?1:filepath.size()==8?8:filepath.size()+1;
    }

    public  class SendOrderViewholder extends BaseViewHolder{
        public ImageView item_sendorder_add;

        public SendOrderViewholder(View itemView) {
            super(itemView);
        }

        @Override
        public void init(View itemview) {
             item_sendorder_add = itemview.findViewById(R.id.item_sendorder_add);

        }
    }



}



